/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DownloaderGUI.java
 *
 * Created on 2012-03-01, 07:21:06
 */
package pl.cezary.butler.walili.client.gui;

import java.util.List;
import java.util.ResourceBundle;
import java.util.Timer;
import javax.swing.JOptionPane;
import pl.cezary.butler.walili.client.LoginControler;
import pl.cezary.butler.walili.client.QueryControler;
import pl.cezary.butler.walili.client.TaskControler;
import pl.cezary.butler.walili.client.UserDataSource;
import pl.cezary.butler.walili.common.Task;
import pl.cezary.butler.walili.common.TaskStatus;

public class DownloaderGUI extends javax.swing.JFrame implements UserDataSource {

    private static final ResourceBundle rb = ResourceBundle.getBundle("gui");
    private final Timer progresTimer;

    /**
     * Creates new form DownloaderGUI
     */
    public DownloaderGUI() {
        initComponents();
        zadania.setEnabled(false);
        progresTimer = TrafficUpdateProgressbarAction.scheduleTrafficLogging(1000, jprogTraffic);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        buttonGroup1 = new javax.swing.ButtonGroup();
        zadania = new pl.cezary.butler.walili.client.gui.ZadaniaGUI();
        jprogTraffic = new javax.swing.JProgressBar();
        jPanel1 = new javax.swing.JPanel();
        jbtnStart = new javax.swing.JButton();
        jbtnStop = new javax.swing.JButton();
        jbtnRetry = new javax.swing.JButton();
        jbtnFillMimeTypes = new javax.swing.JButton();
        jbtnFillDownloads = new javax.swing.JButton();
        zapytanieGUI1 = new pl.cezary.butler.walili.client.gui.ZapytanieGUI();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        java.awt.GridBagLayout jPanel1Layout = new java.awt.GridBagLayout();
        jPanel1Layout.columnWidths = new int[] {0, 14, 0, 14, 0, 14, 0};
        jPanel1Layout.rowHeights = new int[] {0};
        jPanel1.setLayout(jPanel1Layout);

        jbtnStart.setText("Start");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        jPanel1.add(jbtnStart, gridBagConstraints);

        jbtnStop.setText("Stop");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.LINE_START;
        jPanel1.add(jbtnStop, gridBagConstraints);

        jbtnRetry.setText("Retry failed");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 4;
        gridBagConstraints.gridy = 0;
        jPanel1.add(jbtnRetry, gridBagConstraints);

        jbtnFillMimeTypes.setText("Fill mime types");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 6;
        gridBagConstraints.gridy = 0;
        jPanel1.add(jbtnFillMimeTypes, gridBagConstraints);

        jbtnFillDownloads.setText("Fill downloads");
        jPanel1.add(jbtnFillDownloads, new java.awt.GridBagConstraints());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(zapytanieGUI1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jprogTraffic, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(zadania, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 598, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(zadania, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jprogTraffic, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(zapytanieGUI1, javax.swing.GroupLayout.DEFAULT_SIZE, 388, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jbtnFillDownloads;
    private javax.swing.JButton jbtnFillMimeTypes;
    private javax.swing.JButton jbtnRetry;
    private javax.swing.JButton jbtnStart;
    private javax.swing.JButton jbtnStop;
    private javax.swing.JProgressBar jprogTraffic;
    private pl.cezary.butler.walili.client.gui.ZadaniaGUI zadania;
    private pl.cezary.butler.walili.client.gui.ZapytanieGUI zapytanieGUI1;
    // End of variables declaration//GEN-END:variables

    @Override
    public String getHost() {
        return "unsupported";
    }

    @Override
    public String getPassword() {
        return "unsupported";
    }

    @Override
    public String getLogin() {
        return "unsupported";
    }

    @Override
    public String getZadanieCel() {
        return zadania.jTextFieldZadanie.getText();
    }

    @Override
    public String getQueryTarget() {
        return zapytanieGUI1.getQueryTarget();
    }

    @Override
    public String getQueryLocation() {
        return zapytanieGUI1.getQueryLocation();
    }

    @Override
    public TaskStatus getQueryStatus() {
        return zapytanieGUI1.getQueryStatus();
    }

    @Override
    public void enableLogowanie(boolean stan) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void enableZapytanie(boolean stan) {
        //throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void enableZadanie(final boolean stan) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                zadania.setEnabled(stan);
            }
        });
    }

    @Override
    public void setWynikZapytania(final List<Task> wynik) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
//                zapytanie1.jButtonZapytanie.setEnabled(true);
                zapytanieGUI1.setResults(wynik);
            }
        });
    }

    private static String getLocalized(String id) {
        return rb.getString(id);
    }

    @Override
    public String podajZlokalizowane(String id) {
        return getLocalized(id);
    }

    @Override
    public void alert(String komunikat) {
        JOptionPane.showMessageDialog(this, komunikat, getLocalized("alert.text") , JOptionPane.WARNING_MESSAGE);

    }

    @Override
    public void errorInfo(String komunikat) {
        JOptionPane.showMessageDialog(this, komunikat, getLocalized("errorInfo.text"), JOptionPane.ERROR_MESSAGE);
    }

    @Override
    public boolean tryAgain(String komunikat) {
        final String[] options = {getLocalized("tryAgain.yes"), getLocalized("tryAgain.no")};
        int result = JOptionPane.showOptionDialog(this, komunikat, getLocalized("tryAgain.question"), JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
        switch (result) {
            case JOptionPane.YES_OPTION:
                return true;
            case JOptionPane.NO_OPTION:
                return false;
            default:
                throw new IllegalStateException();
        }
    }

    @Override
    public void podlaczKontrolerLogowania(final LoginControler kon) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                jbtnStart.setActionCommand(LoginControler.LOGIN);
                jbtnStop.setActionCommand(LoginControler.LOGOUT);
                jbtnStart.addActionListener(kon);
                jbtnStop.addActionListener(kon);
                addWindowListener(new AppCloseAction(kon, new Runnable() {

                    @Override
                    public void run() {
                        progresTimer.cancel();
                    }
                }));
            }
        });
    }

    @Override
    public void podlaczKontrolerZadan(final TaskControler kon) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                zadania.jButtonZadanie.setActionCommand(TaskControler.SEND);
                zadania.jButtonZadanie.addActionListener(kon);
                jbtnRetry.setActionCommand(TaskControler.RETRY_FAILED);
                jbtnRetry.addActionListener(kon);
                jbtnFillMimeTypes.setActionCommand(TaskControler.FILL_MIME_TYPES);
                jbtnFillMimeTypes.addActionListener(kon);
                jbtnFillDownloads.setActionCommand(TaskControler.FILL_DOWNLOADS);
                jbtnFillDownloads.addActionListener(kon);
            }
        });
    }

    @Override
    public void podlaczKontrolerZapytan(final QueryControler kon) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                zapytanieGUI1.jButtonZapytanie.setActionCommand(QueryControler.SEND);
                zapytanieGUI1.jButtonZapytanie.addActionListener(kon);
            }
        });
    }

    @Override
    public void credentialsNecessary(boolean neccessary) {
    }
}
